from marshmallow import Schema,fields
from application.apps.users.models import User,UserProfile

class UserSchema(Schema):
    name   = fields.String()
    age    = fields.Integer()
    email  = fields.Email()
    money  = fields.Number()
    class Meta:
        fields = ["name","age","money","email","info"]
        ordered = True # 转换成有序字典

def index():
    """序列化"""
    """多个模型数据的序列化"""
    user1 = User(name="xiaoming",  age=15, email="333@qq.com", money=31.50,city='beijing')
    user2 = User(name="xiaohong",  age=16, email="333@qq.com", money=31.50)
    user3 = User(name="xiaopang",  age=17, email="333@qq.com", money=31.50)
    data_list = [user1,user2,user3]
    data1 = UserSchema(many=True).dumps(data_list) # 注意:序列化多个数据对象要加many=True
    print(type(data1),data1)
    return "ok"